package com.hunktimes.wholesale.ui.splash

import android.Manifest
import android.content.DialogInterface
import androidx.lifecycle.lifecycleScope
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.base.BaseRootFragment
import com.hunktimes.wholesale.util.L
import com.hunktimes.wholesale.util.SP
import com.hunktimes.wholesale.widget.PermissionDialog
import com.hunktimes.wholesale.widget.ProtocolDialog
import com.jianrui.msgvision.view.widget.ProtocolRejectDialog
import com.permissionx.guolindev.PermissionX
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Author: Delete
 * Date: 2020/7/29 15:32
 * Description:
 *
 */
class SplashFragment : BaseRootFragment(R.layout.fragment_splash) {

    override fun onStart() {
        super.onStart()

        if (SP.hasAgreeProtocol()) {
            requestPermission()
        } else {
            showProtocolDialog()
        }
    }

    val permissions = mutableListOf<String>(
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA
    )

    fun requestPermission() {
        L.d("requestPermission")
        PermissionX.init(this)
            .permissions(permissions)
            .onExplainRequestReason { scope, deniedList, beforeRequest ->
                val message = "WholeSale需要您同意以下權限才能正常使用:"
                val dialog = PermissionDialog(requireContext(), message, deniedList)
                scope.showRequestReasonDialog(dialog)
            }.onForwardToSettings { scope, deniedList ->
                val message = "您需要去設置中手動開啟以下權限"
                val dialog = PermissionDialog(requireContext(), message, deniedList)
                scope.showForwardToSettingsDialog(dialog)
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    navi2NextPage()
                } else {
                    requestPermission()
                }
            }

    }

    fun navi2NextPage() {
        L.d("navi2NextPage")
        lifecycleScope.launch {
            delay(3000)
            rootNavController.popBackStack()//清空BaceStack(返回栈,这也SplashFrg不再存留栈内)
            rootNavController.navigate(R.id.navigation_home)
        }
    }

    /**
     * 协议弹窗等
     */
    fun showProtocolDialog() {
        ProtocolDialog.Builder(requireContext()).apply {
            layoutResId = R.layout.dialog_protocol
            negativeButtonMsg = "不同意"
            negativeListener = object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    dialog?.dismiss()
                    onDenyProtocol()
                }
            }
            positiveButtonMsg = "同意"
            positiveListener = object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    dialog?.dismiss()
                    //存储 上一次 同意用户协议的 versionCode
                    SP.saveProtocolVersion()
                    requestPermission()
                }
            }
        }.build().show()
    }

    fun onDenyProtocol() {
        ProtocolRejectDialog.Builder(requireContext()).apply {
            positiveButtonMsg = "下一步"
            positiveListener = object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface?, which: Int) {
                    dialog?.dismiss()
                    showProtocolDialog()
                }
            }
        }.build().show()
    }
}